"""
celery配置
"""
from __future__ import absolute_import
import logging

logger = logging.getLogger('log')
import os
from celery import Celery, platforms
from kombu import Exchange
from kombu import Queue
import datetime
from django.utils import timezone
# set the default Django settings module for the 'celery' program.
resulkt = os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pyServerCfg.settings')
from django.conf import settings  # noqa


app = Celery('appServerCode')
# app.now = timezone.now

app.now = datetime.datetime.utcnow
# # 解决时区问题,定时任务启动就循环输
# app.now = datetime.datetime.now

# 用来发现放到 django settings 中的 celery 配置
app.config_from_object('django.conf:settings')

# 用来发现放到django app中的task
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

# 允许root 用户运行celery
platforms.C_FORCE_ROOT = True

# 设置了三个Queue绑定到一个direct类型的exchange上,然后consumer监听所有的队列,消息来了后就轮询调用consumer进行处理.
task_exchange = Exchange('tasks', type='direct')
# 异步任务优先级
task_queues = [Queue('hipri', task_exchange, routing_key='hipri'),
               Queue('midpri', task_exchange, routing_key='midpri'),
               Queue('lopri', task_exchange, routing_key='lopri')]

# from datetime import timedelta
# from celery.schedules import crontab
# # app.conf.update(  # 定时任务
# #     CELERYBEAT_SCHEDULE={
# #         'sum-task': {
# #             'task': 'pyServerCode.tasks.othersScheduledTasks',
# #             # 'schedule': timedelta(minutes=10),  # 每10分组S执行一次
# #             'seconds': timedelta(seconds=10)
# #             # 'args': (5, 6),传参
# #             # days：天
# #             # seconds：秒schedule
# #             # microseconds：微妙
# #             # milliseconds：毫秒
# #             # minutes：分
# #             # hours：小时
# #         },
# #     }
# # )






@app.task(bind=True)
def debug_task(self):
    print('debug-------')
    logger.info('Request: {0!r}'.format(self.request))

